// pages/myincome/myincome.js
const app = getApp()
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
		curPage:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		var that=this;
		console.log(options.type,"类型")
		var data=new Date;
		var month = (data.getMonth() + 1);
		console.log(data.getFullYear(),(data.getMonth()+1),data.getDate())
		month = month < 10 ? '0' + month : month
		var day = new Date(data.getFullYear(), month, 0) //获取当月的最后一天
	//	console.log(day.getDate())
		
		that.setData({
			startdate: data.getFullYear()+"-"+ (data.getMonth() + 1)+"-"+1,
			enddate: data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + day.getDate(),
			type: options.type
		})
		that.getOrderlist();
    },
    onDetailedTap:function(){
        wx.navigateTo({
            url: '../detailed/detailed'
        })
    },

    startDateChange: function (e) {
        var that = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            startdate: e.detail.value,
            curPage:1,
            order_list:[],
        })
        that.getOrderlist();
    },

    endDateChange: function (e) {
        var that = this;
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            enddate: e.detail.value,
            curPage:1,
            order_list:[],
        })
        that.getOrderlist();
    },
    getOrderlist:function(){
        var that = this;
        if(that.data.startdate == undefined || that.data.enddate == undefined){
            util.showSuccess('开始日期和结束日期都需要选择哦','none',false);
            return false;
        }
        var apiUrl = '/api/store/order/order_total_for_mine';
        var apiData = {
            page:that.data.curPage,
            s_date:that.data.startdate,
            e_date:that.data.enddate,
        }
        util.fetchPost(apiUrl,apiData,function(res){
            if (res.state == 1) {
                that.setData({
                    order_amount:res.datas.shouyi
                });
                var order_list = that.data.order_list || [];
                for (var i in res.datas.list) {
                    order_list.push(res.datas.list[i])
                }
                if (res.datas.list.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    order_list: order_list,
                    ready: res.datas.list.length == 0 ? 1 : 2,
                })
            } else {
                util.message(res.datas)
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.curPage + 1
            that.setData({
                curPage: page,
            })
            that.getOrderlist();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})